/*
 * Copyright 2007-2008 Michele Mostarda ( michele.mostarda@gmail.com ).
 * All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package org.asemantics.anthill.utils;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

import java.util.ArrayList;
import java.util.List;

/**
 * Utility task repeating the block of code inside it until a
 * property is setted.
 */
public class UntilTask extends Task {

    private final List<Task> tasks;

    /**
     * The name of the property checked in iteration.
     */
    private String property;

    /**
     * Constructor.
     */
    public UntilTask() {
        tasks = new ArrayList<Task>();
    }

    /**
     * Returns watch dog variable.
     *
     * @return
     */
    public String getProperty() {
        return property;
    }

    /**
     * Sets the watch dog variable.
     *
     * @param property
     */
    public void setProperty(String property) {
        this.property = property;
    }

    /**
     * Allows to add any kind of task.
     *
     * @param task
     */
    public void add(Task task) {
        tasks.add(task);
    }

    public void execute() throws BuildException {
        super.execute();
        for(;;) {
            if( getProject().getProperties().containsKey(property) ) {
                log("interrupted");
                break;
            }
            for(Task task : tasks) {
                task.execute();
            }
        }
    }

}
